package game.seungha;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Chronometer;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;

public class Activity_Play extends Activity 
{
	final long ONE_SEC	= 1000;
	final long ONE_MIN	= ONE_SEC*60;
	final long ONE_HOUR	= ONE_MIN*60;
	final long ONE_DAY	= ONE_HOUR*24;
	
	Spinner 	mSpinner;
	TextView 	mTextView;
	Activity 	mActivity;
	GridView	mGridView;
	
	CountDownTimer 	mTimer;
	TextView 	mPlayTime;
	
	Resources	mResources;
	
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_play);
                
        mActivity 	= this;
        mResources	= mActivity.getResources();

        mGridView = (GridView) this.findViewById(R.id.id_GridView_Number);    
		
        NGInfo gameInfo = NGInfo.getInstance();
        mGridView.setNumColumns(gameInfo.getLevel()+2);
        mGridView.setAdapter(new Adapter_NumberGrid(this));
        
        mPlayTime = (TextView)findViewById(R.id.id_TextView_PlayTime);
        
        this.playInit();
        this.setText_Level();
        this.setText_PlayTime();

    }	
    
    public void onStart()
    {
    	Log.i("DBG", "[Activity_Play]onStart"+" nHour:");
    
    	super.onStart();
    	mTimer.start();
    }  

    protected void onStop() 
    {
        super.onStop();
        mTimer.cancel();
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
    	Log.i("DBG", "[Activity_Play]onActivityResult"+" requestCode:"+requestCode+" resultCode:"+resultCode+" data:"+data);
        
    	if (resultCode == Activity.RESULT_CANCELED) 
	    {
    		this.finish();
	    }
    	
    	if(resultCode == Activity.RESULT_OK)
    	{
            this.playInit();
    	}
        
        
    }
    
    
    
    public void playInit()
    {
    	NGInfo.mTime_Start 	= System.currentTimeMillis();

        this.setText_Level();
        this.setText_PlayTime();

    }
    
    
    public void setText_Level()
    {
    	NGInfo gameInfo = NGInfo.getInstance();
    	    	
    	TextView lvText = (TextView)findViewById(R.id.id_TextView_LevelValue);
    	   		
		switch(gameInfo.getLevel())
		{
			
			case NG.LEVEL_EASY:
			{
				lvText.setText(mResources.getString(R.string.str_com_level_easy));
				lvText.setTextColor(mResources.getColor(R.color.easy));
				break;
			}
			case NG.LEVEL_NORMAL:
			{
				lvText.setText(mResources.getString(R.string.str_com_level_normal));
				lvText.setTextColor(mResources.getColor(R.color.normal));
				break;
			}
			case NG.LEVEL_HARD:
			{
				lvText.setText(mResources.getString(R.string.str_com_level_hard));
				lvText.setTextColor(mResources.getColor(R.color.hard));
				break;
			}
			default:
			{
				break;
			}
		}
		
    }
    
    public void incorrectView_Draw()
    {
    	TextView incorrectCount = (TextView)findViewById(R.id.id_TextView_IncorrectValue);
    	String strIncorrectCount = String.format("%03d", NGInfo.mIncorrect);
    	  	
    	incorrectCount.setText(strIncorrectCount);
    	
    }
    
    
    
    private void setText_PlayTime()
    {
    	NGInfo.mTime_Start 	= System.currentTimeMillis(); 

    	mTimer = new CountDownTimer(ONE_DAY, 10) 
        {
            public void onTick(long millisUntilFinished) 
            {
        		long currentTime;
        		long differTime;
        		long nHour;
        		long nMin;
        		long nSec;
        		long nMSec;
        		
        		String strPlayTime;
        		
        		currentTime	= System.currentTimeMillis();
        		differTime	= currentTime-NGInfo.mTime_Start;
        		
        		nHour	= (differTime%ONE_DAY)/ONE_HOUR;
        		nMin	= (differTime%ONE_HOUR)/ONE_MIN;
        		nSec	= (differTime%ONE_MIN)/ONE_SEC;
        		nMSec	= differTime%ONE_SEC;
        		
        		//Log.i("DBG", "[Listener_ChronometerTick]onChronometerTick"+" nHour:"+nHour+" nMin:"+nMin+" nSec:"+nSec+" nMSec:"+nMSec);
        		
        		strPlayTime	= String.format("%02d:%02d:%02d.%03d", nHour, nMin, nSec, nMSec);
        		mPlayTime.setText(strPlayTime);            	
            }
            public void onFinish() 
            {
            	
            }
        };
        
        //mTimer.start();
    }
    
}
